package com.das.mechanic_base.adapter.main;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.main.CarBrandBean;

import java.util.ArrayList;
import java.util.List;

public class X3ShowCarmileAdapter extends RecyclerView.Adapter<X3ShowCarmileAdapter.SelectBrandHolder> {

    private Context mContext;

    private List<CarBrandBean> mList;

    private List<Integer> ml = new ArrayList<>();

    public X3ShowCarmileAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
    }

    public void changeSelect(List<CarBrandBean> mLists) {
        this.mList = mLists;
        this.ml.clear();
        notifyDataSetChanged();
    }

    public void changeReset() {
        this.ml.clear();
        for(CarBrandBean carBrandBean :mList){
            carBrandBean.setSelect(false);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public SelectBrandHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new SelectBrandHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_select_brand_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull SelectBrandHolder holder, int position) {

        holder.tv_brand.setText(mList.get(position).getLabel() + "");

        if (mList.get(position).isSelect()) {
            holder.tv_brand.setTextColor(Color.parseColor("#0077ff"));
            holder.tv_brand.setBackground(mContext.getDrawable(R.drawable.x3_shape_idea_select_item));
        } else {
            holder.tv_brand.setTextColor(Color.parseColor("#666666"));
            holder.tv_brand.setBackground(mContext.getDrawable(R.drawable.x3_shape_idea_unselect_item));
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              /*  如果多选，必须至少保留一个，那么替换注释的代码
                if (mList.get(position).isSelect() && ml.size() == 1) {
                    mList.get(position).setSelect(true);
                } else if (mList.get(position).isSelect() && ml.size() != 1) {
                    mList.get(position).setSelect(false);
                } else {
                    mList.get(position).setSelect(true);
                }*/
                if(mList.get(position).isSelect()){
                    mList.get(position).setSelect(false);
                }else {
                    mList.get(position).setSelect(true);
                }
                ml.clear();
                for (CarBrandBean carBrandBean : mList) {
                    if (carBrandBean.isSelect()) {
                        ml.add(1);
                    }
                }
                if (iOnClickSelect != null) {
                    iOnClickSelect.iOnClickSelectBrand(mList, ml.size());
                }
                notifyDataSetChanged();
            }
        });


    }

    IOnClickSelect iOnClickSelect;

    public void setiOnClickSelect(IOnClickSelect iOnClickSelect) {
        this.iOnClickSelect = iOnClickSelect;
    }

    public interface IOnClickSelect {
        void iOnClickSelectBrand(List<CarBrandBean> carBrandBean, int position);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class SelectBrandHolder extends RecyclerView.ViewHolder {
        TextView tv_brand;

        public SelectBrandHolder(@NonNull View itemView) {
            super(itemView);
            tv_brand = itemView.findViewById(R.id.tv_brand);
        }
    }
}
